home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH10 / SRC / SURFACE1.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-02  |  15.4 KB  |  524 lines

  1. VERSION 4.00
  2. Begin VB.Form SurfaceForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Surfaces"
  6.    ClientHeight    =   5700
  7.    ClientLeft      =   300
  8.    ClientTop       =   855
  9.    ClientWidth     =   9090
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6390
  21.    KeyPreview      =   -1  'True
  22.    Left            =   240
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   5700
  25.    ScaleWidth      =   9090
  26.    Top             =   225
  27.    Width           =   9210
  28.    Begin VB.CheckBox ShowAxesCheck 
  29.       Caption         =   "Show Axes"
  30.       Height          =   255
  31.       Left            =   7080
  32.       TabIndex        =   16
  33.       Top             =   3360
  34.       Width           =   2055
  35.    End
  36.    Begin VB.OptionButton Choice 
  37.       Caption         =   "Saddle"
  38.       Height          =   255
  39.       Index           =   8
  40.       Left            =   7080
  41.       TabIndex        =   15
  42.       Top             =   2880
  43.       Width           =   2055
  44.    End
  45.    Begin VB.OptionButton Choice 
  46.       Caption         =   "Cone"
  47.       Height          =   255
  48.       Index           =   7
  49.       Left            =   7080
  50.       TabIndex        =   14
  51.       Top             =   2520
  52.       Width           =   2055
  53.    End
  54.    Begin VB.OptionButton Choice 
  55.       Caption         =   "Holes"
  56.       Height          =   255
  57.       Index           =   6
  58.       Left            =   7080
  59.       TabIndex        =   13
  60.       Top             =   2160
  61.       Width           =   2055
  62.    End
  63.    Begin VB.TextBox PhiText 
  64.       Height          =   285
  65.       Left            =   3600
  66.       TabIndex        =   12
  67.       Text            =   "0.1570"
  68.       Top             =   5400
  69.       Width           =   855
  70.    End
  71.    Begin VB.TextBox ThetaText 
  72.       Height          =   285
  73.       Left            =   2040
  74.       TabIndex        =   10
  75.       Text            =   "0.6283"
  76.       Top             =   5400
  77.       Width           =   855
  78.    End
  79.    Begin VB.TextBox RText 
  80.       Height          =   285
  81.       Left            =   480
  82.       TabIndex        =   8
  83.       Text            =   "10"
  84.       Top             =   5400
  85.       Width           =   855
  86.    End
  87.    Begin VB.OptionButton Choice 
  88.       Caption         =   "Hemisphere"
  89.       Height          =   255
  90.       Index           =   5
  91.       Left            =   7080
  92.       TabIndex        =   7
  93.       Top             =   1800
  94.       Width           =   2055
  95.    End
  96.    Begin VB.OptionButton Choice 
  97.       Caption         =   "Randomized Ridges"
  98.       Height          =   255
  99.       Index           =   4
  100.       Left            =   7080
  101.       TabIndex        =   6
  102.       Top             =   1440
  103.       Width           =   2055
  104.    End
  105.    Begin VB.OptionButton Choice 
  106.       Caption         =   "Ridges"
  107.       Height          =   255
  108.       Index           =   3
  109.       Left            =   7080
  110.       TabIndex        =   5
  111.       Top             =   1080
  112.       Width           =   2055
  113.    End
  114.    Begin VB.OptionButton Choice 
  115.       Caption         =   "Bowl"
  116.       Height          =   255
  117.       Index           =   2
  118.       Left            =   7080
  119.       TabIndex        =   4
  120.       Top             =   720
  121.       Width           =   2055
  122.    End
  123.    Begin VB.OptionButton Choice 
  124.       Caption         =   "Mounds"
  125.       Height          =   255
  126.       Index           =   1
  127.       Left            =   7080
  128.       TabIndex        =   3
  129.       Top             =   360
  130.       Width           =   2055
  131.    End
  132.    Begin VB.OptionButton Choice 
  133.       Caption         =   "Splash"
  134.       Height          =   255
  135.       Index           =   0
  136.       Left            =   7080
  137.       TabIndex        =   2
  138.       Top             =   0
  139.       Value           =   -1  'True
  140.       Width           =   2055
  141.    End
  142.    Begin VB.PictureBox Pict 
  143.       AutoRedraw      =   -1  'True
  144.       Height          =   5295
  145.       Left            =   0
  146.       ScaleHeight     =   349
  147.       ScaleMode       =   3  'Pixel
  148.       ScaleWidth      =   461
  149.       TabIndex        =   0
  150.       Top             =   0
  151.       Width           =   6975
  152.    End
  153.    Begin MSComDlg.CommonDialog LoadDialog 
  154.       Left            =   7080
  155.       Top             =   4560
  156.       _version        =   65536
  157.       _extentx        =   847
  158.       _extenty        =   847
  159.       _stockprops     =   0
  160.       cancelerror     =   -1  'True
  161.    End
  162.    Begin VB.Label Label1 
  163.       Caption         =   "Phi"
  164.       Height          =   255
  165.       Index           =   2
  166.       Left            =   3240
  167.       TabIndex        =   11
  168.       Top             =   5400
  169.       Width           =   375
  170.    End
  171.    Begin VB.Label Label1 
  172.       Caption         =   "Theta"
  173.       Height          =   255
  174.       Index           =   1
  175.       Left            =   1440
  176.       TabIndex        =   9
  177.       Top             =   5400
  178.       Width           =   495
  179.    End
  180.    Begin VB.Label Label1 
  181.       Caption         =   "R"
  182.       Height          =   255
  183.       Index           =   0
  184.       Left            =   240
  185.       TabIndex        =   1
  186.       Top             =   5400
  187.       Width           =   255
  188.    End
  189.    Begin VB.Menu mnuFile 
  190.       Caption         =   "&File"
  191.       Begin VB.Menu mnuFileLoad 
  192.          Caption         =   "&Load..."
  193.          Shortcut        =   ^L
  194.       End
  195.       Begin VB.Menu mnuFileSaveAs 
  196.          Caption         =   "&Save As..."
  197.          Shortcut        =   ^A
  198.       End
  199.       Begin VB.Menu mnuFileSep 
  200.          Caption         =   "-"
  201.       End
  202.       Begin VB.Menu mnuFileExit 
  203.          Caption         =   "E&xit"
  204.       End
  205.    End
  206. Attribute VB_Name = "SurfaceForm"
  207. Attribute VB_Creatable = False
  208. Attribute VB_Exposed = False
  209. Option Explicit
  210. ' Location of viewing eye.
  211. Dim EyeR As Single
  212. Dim EyeTheta As Single
  213. Dim EyePhi As Single
  214. Const Dtheta = PI / 20
  215. Const Dphi = PI / 20
  216. Const Dr = 1
  217. ' Location of focus point.
  218. Const FocusX = 0#
  219. Const FocusY = 0#
  220. Const FocusZ = 0#
  221. Dim Projector(1 To 4, 1 To 4) As Single
  222. Dim ThePicture As ObjPicture
  223. Dim ShowingParameters As Boolean
  224. Dim ChoiceNum As Integer
  225. ' *******************************************************
  226. ' Rotate the points in the cube and draw the cube.
  227. ' *******************************************************
  228. Private Sub DrawData(pic As Object)
  229. Dim x As Single
  230. Dim y As Single
  231. Dim z As Single
  232. Dim S(1 To 4, 1 To 4) As Single
  233. Dim t(1 To 4, 1 To 4) As Single
  234. Dim ST(1 To 4, 1 To 4) As Single
  235. Dim PST(1 To 4, 1 To 4) As Single
  236.     MousePointer = vbHourglass
  237.     Refresh
  238.     ' Prevent overflow errors when drawing lines
  239.     ' too far out of bounds.
  240.     On Error Resume Next
  241.     ' Scale and translate so it looks OK in pixels.
  242.     m3Scale S, 35, -35, 1
  243.     m3Translate t, 230, 175, 0
  244.     m3MatMultiplyFull ST, S, t
  245.     m3MatMultiplyFull PST, Projector, ST
  246.     ' Transform the points.
  247.     ThePicture.ApplyFull PST
  248.     ' Display the data.
  249.     pic.Cls
  250.     ThePicture.Draw pic, EyeR
  251.     pic.Refresh
  252.     ' Display the viewnig parameters.
  253.     ShowViewingParameters
  254.     MousePointer = vbDefault
  255. End Sub
  256. Sub ShowViewingParameters()
  257.     ShowingParameters = True
  258.     RText.Text = Format$(EyeR, "0.0000")
  259.     ThetaText.Text = Format$(EyeTheta, "0.0000")
  260.     PhiText.Text = Format$(EyePhi, "0.0000")
  261.     RText.Refresh
  262.     ThetaText.Refresh
  263.     PhiText.Refresh
  264.     ShowingParameters = False
  265. End Sub
  266. Private Sub Choice_Click(Index As Integer)
  267.     ChoiceNum = Index
  268.     CreateData (ShowAxesCheck.value = vbChecked)
  269.     DrawData Pict
  270.     Pict.SetFocus
  271. End Sub
  272. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  273.     Select Case KeyCode
  274.         Case vbKeyLeft
  275.             EyeTheta = EyeTheta - Dtheta
  276.         
  277.         Case vbKeyRight
  278.             EyeTheta = EyeTheta + Dtheta
  279.         
  280.         Case vbKeyUp
  281.             EyePhi = EyePhi - Dphi
  282.         
  283.         Case vbKeyDown
  284.             EyePhi = EyePhi + Dphi
  285.                 
  286.         Case Else
  287.             Exit Sub
  288.     End Select
  289.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  290.     DrawData Pict
  291. End Sub
  292. Private Sub Form_KeyPress(KeyAscii As Integer)
  293.     Select Case KeyAscii
  294.         Case Asc("+")
  295.             EyeR = EyeR + Dr
  296.         
  297.         Case Asc("-")
  298.             EyeR = EyeR - Dr
  299.         
  300.         Case Else
  301.             Exit Sub
  302.     End Select
  303.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  304.     DrawData Pict
  305. End Sub
  306. Private Sub Form_Load()
  307.     ' Initialize the eye position.
  308.     EyeR = 10
  309.     EyeTheta = PI * 0.2
  310.     EyePhi = PI * 0.1
  311.     ' Initialize the projection transformation.
  312.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  313.     ' Create the data.
  314.     CreateData (ShowAxesCheck.value = vbChecked)
  315.     ' Project and draw the data.
  316.     Me.Show
  317.     DrawData Pict
  318. End Sub
  319. ' ************************************************
  320. ' Create the surface.
  321. ' ************************************************
  322. Sub CreateData(show_axes As Boolean)
  323. Const Xmin = -5
  324. Const Zmin = -5
  325. Const Dx = 0.3
  326. Const Dz = 0.3
  327. Const NumX = -2 * Xmin / Dx
  328. Const NumZ = -2 * Zmin / Dz
  329. Const Amp = 0.25
  330. Const Per = 2 * PI / 4
  331. Const Amp2 = 1
  332. Const Per2 = 2 * PI / 16
  333. Const Amp3 = 2
  334. Dim grid As ObjGrid3D
  335. Dim axis As ObjPolyline
  336. Dim i As Integer
  337. Dim j As Integer
  338. Dim x As Single
  339. Dim y As Single
  340. Dim z As Single
  341. Dim D As Single
  342. Dim R2 As Single
  343. Dim x1 As Single
  344. Dim z1 As Single
  345. Dim x2 As Single
  346. Dim z2 As Single
  347.     MousePointer = vbHourglass
  348.     Refresh
  349.     Set ThePicture = New ObjPicture
  350.     Set grid = New ObjGrid3D
  351.     grid.SetBounds Xmin, Dx, NumX, Zmin, Dz, NumZ
  352.     ThePicture.objects.Add grid
  353.     If show_axes Then
  354.         Set axis = New ObjPolyline
  355.         ThePicture.objects.Add axis
  356.         axis.AddSegment 0, 0, 0, 5.5, 0, 0
  357.         axis.AddSegment 0, 0, 0, 0, 3, 0
  358.         axis.AddSegment 0, 0, 0, 0, 0, 5.5
  359.     End If
  360.     R2 = (Xmin + 3 * Dx) * (Xmin + 3 * Dx)
  361.     x = Xmin
  362.     For i = 1 To NumX
  363.         z = Zmin
  364.         For j = 1 To NumZ
  365.             Select Case ChoiceNum
  366.                 Case 0  ' Splash.
  367.                     D = Sqr(x * x + z * z)
  368.                     y = Amp * Cos(3 * D)
  369.                 
  370.                 Case 1  ' Mounds.
  371.                     y = Amp * (Cos(Per * x) + Cos(Per * z))
  372.                 
  373.                 Case 2  ' Bowl.
  374.                     y = 0.2 * (x * x + z * z) - 5#
  375.                 
  376.                 Case 3  ' Ridges.
  377.                     y = Amp2 * Cos(Per2 * x) + Amp3 * Cos(Per * z) / (Abs(z) / 3 + 1)
  378.             
  379.                 Case 4  ' Random ridges.
  380.                     y = Amp2 * Cos(Per2 * x) + Amp3 * Cos(Per * z) / (Abs(z) / 3 + 1) + Amp * Rnd
  381.             
  382.                 Case 5  ' Hemisphere.
  383.                     D = x * x + z * z
  384.                     If D >= R2 Then
  385.                         y = 0
  386.                     Else
  387.                         y = Sqr(R2 - D)
  388.                     End If
  389.                 
  390.                 Case 6  ' Holes.
  391.                     x1 = (x + Xmin / 2)
  392.                     z1 = (z + Xmin / 2)
  393.                     x2 = (x - Xmin / 2)
  394.                     z2 = (z - Xmin / 2)
  395.                     y = Amp3 - _
  396.                 1 / (x1 * x1 + z1 * z1 + 0.1) - _
  397.                 1 / (x2 * x2 + z1 * z1 + 0.1) - _
  398.                 1 / (x1 * x1 + z2 * z2 + 0.1) - _
  399.                 1 / (x2 * x2 + z2 * z2 + 0.1)
  400.             
  401.                 Case 7  ' Cone.
  402.                     D = 2 * (Amp3 - Sqr(x * x + z * z))
  403.                     If D < -Amp3 Then D = -Amp3
  404.                     y = D
  405.             
  406.                 Case 8  ' Saddle.
  407.                     y = (x * x - z * z) / 10
  408.                 
  409.             End Select
  410.             
  411.             grid.SetValue x, y, z
  412.             z = z + Dz
  413.         Next j
  414.         x = x + Dx
  415.     Next i
  416.     MousePointer = vbDefault
  417. End Sub
  418. Private Sub mnuFileExit_Click()
  419.     Unload Me
  420. End Sub
  421. Private Sub mnuFileLoad_Click()
  422. Dim fname As String
  423. Dim filenum As Integer
  424. Dim txt As String
  425. Dim Xmin As Single
  426. Dim Ymin As Single
  427. Dim xmax As Single
  428. Dim ymax As Single
  429.     ' Allow the user to pick a file.
  430.     On Error Resume Next
  431.     LoadDialog.filename = "*.APF"
  432.     LoadDialog.ShowOpen
  433.     If Err.Number = cdlCancel Then
  434.         Unload LoadDialog
  435.         Exit Sub
  436.     ElseIf Err.Number <> 0 Then
  437.         Unload LoadDialog
  438.         Beep
  439.         MsgBox "Error selecting file.", , vbExclamation
  440.         Exit Sub
  441.     End If
  442.     On Error GoTo 0
  443.     fname = LoadDialog.filename
  444.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  445.         - Len(LoadDialog.FileTitle) - 1)
  446.     ' Clear the picture.
  447.     Set ThePicture = Nothing
  448.     ' Open the file.
  449.     filenum = FreeFile
  450.     Open fname For Input As #filenum
  451.     ' Make sure it's an Object Picture File.
  452.     Input #filenum, txt
  453.     If txt <> "3D APF PICTURE" Then
  454.         Close filenum
  455.         Beep
  456.         MsgBox "Error reading file """ & fname & """.", , vbExclamation
  457.         Exit Sub
  458.     End If
  459.     ' Read the picture.
  460.     Set ThePicture = New ObjPicture
  461.     ThePicture.FileInput filenum
  462.     ' Close the file.
  463.     Close filenum
  464.     ' Refresh the display.
  465.     DrawData Pict
  466.     ' Deselect all the option buttons.
  467.     For ChoiceNum = 0 To 8
  468.         If Choice(ChoiceNum).value Then _
  469.             Choice(ChoiceNum).value = False
  470.     Next ChoiceNum
  471. End Sub
  472. Private Sub mnuFileSaveAs_Click()
  473. Dim fname As String
  474. Dim filenum As Integer
  475.     ' Allow the user to pick a file.
  476.     On Error Resume Next
  477.     LoadDialog.filename = "*.APF"
  478.     LoadDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  479.     LoadDialog.ShowSave
  480.     If Err.Number = cdlCancel Then
  481.         Unload LoadDialog
  482.         Exit Sub
  483.     ElseIf Err.Number <> 0 Then
  484.         Unload LoadDialog
  485.         Beep
  486.         MsgBox "Error selecting file.", , vbExclamation
  487.         Exit Sub
  488.     End If
  489.     On Error GoTo 0
  490.     fname = LoadDialog.filename
  491.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  492.         - Len(LoadDialog.FileTitle) - 1)
  493.     ' Open the file.
  494.     filenum = FreeFile
  495.     Open fname For Output As #filenum
  496.     ' Write the picture.
  497.     ThePicture.FileWrite filenum
  498.     ' Close the file.
  499.     Close filenum
  500. End Sub
  501. Private Sub PhiText_Change()
  502.     If ShowingParameters Then Exit Sub
  503.     EyePhi = CSng(PhiText.Text)
  504.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  505.     DrawData Pict
  506. End Sub
  507. Private Sub RText_Change()
  508.     If ShowingParameters Then Exit Sub
  509.     EyeR = CSng(RText.Text)
  510.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  511.     DrawData Pict
  512. End Sub
  513. Private Sub ShowAxesCheck_Click()
  514.     CreateData (ShowAxesCheck.value = vbChecked)
  515.     DrawData Pict
  516.     Pict.SetFocus
  517. End Sub
  518. Private Sub ThetaText_Change()
  519.     If ShowingParameters Then Exit Sub
  520.     EyeTheta = CSng(ThetaText.Text)
  521.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  522.     DrawData Pict
  523. End Sub
  524.